package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.common.UserHolder;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/sys_config")
@Slf4j
public class SysConfigController {

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 所有配置数据
     *
     * @return
     */
    @GetMapping("/all")
    public ResultData all() {
        Map<String, Object> map = sysConfigService.all();
        return ResultData.ok("", map);
    }

    /**
     * 修改配置
     *
     * @param configs
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody List<SysConfig> configs) {
        LambdaQueryWrapper<SysRole> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //获取登录人员信息，把test类型转化为string，去掉[]和""
        String userId = UserHolder.getUserId();
        SysUser user2 = sysUserService.getById(userId);
        String strip = StringUtils.strip(user2.getRoleId(), "[]");
        String[] split = strip.split(",");
        for (int i = 0; i < split.length; i++) {
            split[i] = StringUtils.replace(split[i], "\"", "");
        }
        roleLambdaQueryWrapper.in(SysRole::getId, split);
        List<SysRole> list1 = sysRoleService.list(roleLambdaQueryWrapper);
        List<String> collect = list1.stream().map((item) -> {
            return item.getId();
        }).collect(Collectors.toList());
        //根据权限id去查询是否有用户管理权限
        LambdaQueryWrapper<SysRoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.in(SysRoleMenu::getRoleId, collect);
        List<SysRoleMenu> list = sysRoleMenuService.list(roleMenuLambdaQueryWrapper);
        for (SysRoleMenu roleMenu : list) {
            String menuId = roleMenu.getMenuId();
            SysMenu menu = sysMenuService.getById(menuId);
            //一级权限
            if ("系统设置".equals(menu.getTitle())) {
                LambdaQueryWrapper<SysMenu> menuLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                menuLambdaQueryWrapper1.eq(SysMenu::getPid, menuId);
                List<SysMenu> list3 = sysMenuService.list(menuLambdaQueryWrapper1);
                for (SysMenu Menu2 : list3) {
                    //二级权限
                    if ("系统配置".equals(Menu2.getTitle())) {
                        LambdaQueryWrapper<SysMenu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
                        menuLambdaQueryWrapper.eq(SysMenu::getPid, Menu2.getId());
                        List<SysMenu> list2 = sysMenuService.list(menuLambdaQueryWrapper);
                        for (SysMenu Menu1 : list2) {
                            //三级权限
                            if ("保存".equals(Menu1.getTitle())) {
                                //更新数据
                                for (SysConfig config : configs) {
                                    sysConfigService.updateById(config);
                                }
                                return ResultData.ok("更新成功", "更新成功");
                            }
                        }
                    }
                }
            }
        }
        //否则没有权限添加
        return ResultData.error("更新失败,无权限更新!");
    }


}
